package com.zhentao.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhentao.domain.SysPowerBattery;
import com.zhentao.domain.SysPowerCabinet;
import com.zhentao.domain.SysPreferential;
import com.zhentao.domain.SysReservation;
import com.zhentao.domain.infoVo.PreferentialVo;
import com.zhentao.domain.vo.Result;
import com.zhentao.mapper.SysPreferentialMapper;
import com.zhentao.service.SysPowerBatteryService;
import com.zhentao.service.SysPowerCabinetService;
import com.zhentao.service.SysReservationService;
import com.zhentao.mapper.SysReservationMapper;
import com.zhentao.utils.AuthContextUtil;
import com.zhentao.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
* @author 86133
* @description 针对表【sys_reservation】的数据库操作Service实现
* @createDate 2024-08-28 18:42:23
*/
@Service
public class SysReservationServiceImpl extends ServiceImpl<SysReservationMapper, SysReservation>
    implements SysReservationService{
    @Autowired
    private SysPowerCabinetService sysPowerCabinetService;
    @Autowired
    private SysPowerBatteryService powerBatteryService;
    @Autowired
    private UserUtils userUtils;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private SysReservationMapper sysReservationMapper;
    @Override
    public Result saveReservation(SysReservation savedReservation) {
        SysReservation one = this.getOne(Wrappers.<SysReservation>lambdaQuery().eq(SysReservation::getUid, userUtils.GetUserId()));
        if (one != null){
            return Result.build(null, 500,"您已预约过");
        }

        List<SysPowerCabinet> list = sysPowerCabinetService.list(Wrappers.<SysPowerCabinet>lambdaQuery().eq(SysPowerCabinet::getSid, savedReservation.getSid()));
        boolean flag = false;
        if (list.size() > 0) {
            savedReservation.setUid(userUtils.GetUserId());
            for (SysPowerCabinet sysPowerCabinet : list) {
                List<SysPowerBattery> list1 = powerBatteryService.list(Wrappers.<SysPowerBattery>lambdaQuery().eq(SysPowerBattery::getPcid, sysPowerCabinet.getId()));
                for (SysPowerBattery sysPowerBattery : list1) {
                    if (Objects.equals(sysPowerBattery.getBid(), savedReservation.getBid()) && sysPowerBattery.getNum() > 0){
                        sysPowerBattery.setNum(sysPowerBattery.getNum() - 1);
                        powerBatteryService.updateById(sysPowerBattery);
                        savedReservation.setGid(sysPowerCabinet.getId());
                        savedReservation.setCreateTime(new Date());
                        flag = true;
                        break;
                    }
//                    else {
//                        return Result.build(null, 500,"预约失败");
//                    }
                }
                if (flag) {
                    break;
                }
            }
            boolean save = this.save(savedReservation);
            UUID uuid = UUID.randomUUID();
            stringRedisTemplate.opsForValue().set(uuid.toString(), savedReservation.getId().toString(),20, TimeUnit.MINUTES);
            return save? Result.build(uuid.toString(), 200,"预约成功") : Result.build(null, 500,"预约失败");
        }else {
            return Result.build(null, 500,"预约失败");
        }
    }

    @Override
    public Result ReservationTime(String uuid) {
        String s = stringRedisTemplate.opsForValue().get(uuid);
        if (s != null){
            Integer id = Integer.parseInt(s);
            SysReservation byId = this.getById(id);
            if (byId != null){
                Long expire = stringRedisTemplate.opsForValue().getOperations().getExpire(uuid);

                return Result.build(expire/60, 200, "操作成功");
            }else {
                return Result.build(null, 500,"预约信息不存在");
            }
        }else {
            int uid = userUtils.GetUserId();
            SysReservation byId = this.getOne(Wrappers.<SysReservation>lambdaQuery().eq(SysReservation::getUid, uid));
            if (byId != null){
                Integer gid = byId.getGid();
                Integer bid = byId.getBid();
                List<SysPowerBattery> list = powerBatteryService.list(Wrappers.<SysPowerBattery>lambdaQuery().eq(SysPowerBattery::getPcid, gid));
                for (SysPowerBattery sysPowerBattery : list) {
                    if (Objects.equals(sysPowerBattery.getBid(), bid)){
                        sysPowerBattery.setNum(sysPowerBattery.getNum() + 1);
                        powerBatteryService.updateById(sysPowerBattery);
                    }
                }
            }
            this.remove(Wrappers.<SysReservation>lambdaQuery().eq(SysReservation::getUid, uid));
            return Result.build(null, 500,"预约过期");
        }
    }
//查询预约信息
    @Override
    public Result getReservation() {
        int i = userUtils.GetUserId();
        PreferentialVo allById = sysReservationMapper.getAllById(i);
        if (allById == null){
            return Result.build(null,500,"您还未预约");
        }
        return Result.build(allById,200,"操作成功");
    }
}




